package com.efast.dmom.equipment.service.checking.impl;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.EquipCheckingProgramStandard;
import com.efast.dmom.equipment.bean.EquipHomeTaskParamBean;
import com.efast.dmom.equipment.bean.checkplan.EquipCheckPlanBean;
import com.efast.dmom.equipment.bean.checkplan.EquipCheckingTaskEquipBean;
import com.efast.dmom.equipment.bean.checkplan.EquipCheckingTaskProgramBean;
import com.efast.dmom.equipment.dao.checking.EquipCheckingTaskDao;
import com.efast.dmom.equipment.entity.*;
import com.efast.dmom.equipment.service.checking.IEquipCheckingOrderService;
import com.efast.dmom.equipment.service.checking.IEquipCheckingPlanService;
import com.efast.dmom.equipment.util.ExplanPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EquipCheckingPlanServiceImpl implements IEquipCheckingPlanService {

	@Autowired
	private JdbcDao jdbcDao;
	@Autowired
	private EquipCheckingTaskDao equipCheckingTaskDao;
	@Autowired
	private IEquipCheckingOrderService equipCheckingService;
	@Override
	public void addCheckingPlan(EquipCheckPlanBean bean) {

		//验证计划名字唯一
		List<Integer> planIdList=equipCheckingTaskDao.queryPlanNameUnique(bean.getPlanName(),bean.getPlanStartTime(),bean.getPlanEndTime());
        if(planIdList!=null&&planIdList.size()>0){
        	for(int i=0;i<planIdList.size();i++){
				List<EquipCheckingTaskEquipBean> eList=equipCheckingTaskDao.selectCheckTaskProgramByIds(planIdList.get(i));
				if(eList!=null&&eList.size()>0&&bean.getEquipProgram()!=null&&bean.getEquipProgram().size()>0){
					for(int a=0;a<eList.size();a++){
						for(int b=0;b<bean.getEquipProgram().size();b++){
							if(eList.get(a).getEquipCode().equals(bean.getEquipProgram().get(b).getEquipCode())){
								throw new ServiceException("计划重复");
							}
						}
					}
				}
			}
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		EquipCheckingPlan b=new EquipCheckingPlan();

		b.setType(bean.getType());
		b.setUnit(bean.getUnit());
		b.setFrequency(bean.getFrequency());
		try {
			b.setPlanStartTime(sdf.parse(bean.getPlanStartTime()));
			b.setPlanEndTime(sdf.parse(bean.getPlanEndTime()));
		} catch (ParseException e) {
			throw new ServiceException("时间格式异常");
		}
		b.setPlanName(bean.getPlanName());
		b.setDetail(bean.getDetail());
		b.setStatus(bean.getStatus());
		b.setOrgId(bean.getOrgId());
		b.setOrgName(bean.getOrgName());
		b.setAuditor(bean.getAuditor());
		b.setCreateTime(new Date());
		b.setLastUpdateTime(new Date());
		b.setCreateUser(SpringWebUtils.getRequestUser());
		b.setLastUpdateUser(SpringWebUtils.getRequestUser());
		b.setCompanyCode(SpringWebUtils.getRequestCompany());
		//保存计划
        equipCheckingTaskDao.addEquipCheckPlan(b);
        if("submission".equals(b.getStatus())) {
			if(bean.getEquipProgram()==null||bean.getEquipProgram().size()==0){
				throw new ServiceException("请选择设备");
			}
			for(int k=0;k<bean.getEquipProgram().size();k++){
				if(bean.getEquipProgram().get(k).getProgramList()==null||bean.getEquipProgram().get(k).getProgramList().size()==0){
					throw new ServiceException("第"+(k+1)+"行设备未选择项目");
				}
			}
        	bean.setId(b.getId());
			equipCheckingService.addCheckingTask(bean);
		}else{
			bean.setId(b.getId());
			this.addCheckingTaskEquip(bean);
		}

	}

	@Override
	public void deleteCheckingPlan(EquipCheckPlanBean bean) {
		equipCheckingTaskDao.deleteEquipCheckPlan(bean.getId());
		this.deleteCheckingTaskEquip(bean);
	}

	@Override
	public void updateCheckingPlan(EquipCheckPlanBean bean) {
		//验证计划名字唯一
		//验证计划名字唯一
		List<Integer> planIdList=equipCheckingTaskDao.queryPlanNameUnique(bean.getPlanName(),bean.getPlanStartTime(),bean.getPlanEndTime());
		if(planIdList!=null&&planIdList.size()>0){
			for(int i=0;i<planIdList.size();i++){
				List<EquipCheckingTaskEquipBean> eList=equipCheckingTaskDao.selectCheckTaskProgramByIds(planIdList.get(i));
				if(!planIdList.get(i).equals(bean.getId())&&eList!=null&&eList.size()>0&&bean.getEquipProgram()!=null&&bean.getEquipProgram().size()>0){
					for(int a=0;a<eList.size();a++){
						for(int b=0;b<bean.getEquipProgram().size();b++){
							if(eList.get(a).getEquipCode().equals(bean.getEquipProgram().get(b).getEquipCode())){
								throw new ServiceException("计划重复");
							}
						}
					}
				}
			}
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		EquipCheckingPlan b=new EquipCheckingPlan();
		b.setType(bean.getType());
		b.setUnit(bean.getUnit());
		b.setFrequency(bean.getFrequency());
		try {
			b.setPlanStartTime(sdf.parse(bean.getPlanStartTime()));
			b.setPlanEndTime(sdf.parse(bean.getPlanEndTime()));
		} catch (ParseException e) {
			throw new ServiceException("时间格式异常");
		}
		b.setId(bean.getId());
		b.setPlanName(bean.getPlanName());
		b.setDetail(bean.getDetail());
		b.setStatus(bean.getStatus());
		b.setOrgId(bean.getOrgId());
		b.setOrgName(bean.getOrgName());
		b.setAuditor(bean.getAuditor());
		b.setCreateTime(new Date());
		b.setLastUpdateTime(new Date());
		b.setCreateUser(SpringWebUtils.getRequestUser());
		b.setLastUpdateUser(SpringWebUtils.getRequestUser());
		b.setCompanyCode(SpringWebUtils.getRequestCompany());
		//保存计划
		equipCheckingTaskDao.updateEquipCheckPlan(b);
		//删除前面的任务设备
		this.deleteCheckingTaskEquip(bean);
		if("submission".equals(b.getStatus())) {
			if(bean.getEquipProgram()==null||bean.getEquipProgram().size()==0){
				throw new ServiceException("请选择设备");
			}
			for(int k=0;k<bean.getEquipProgram().size();k++){
				if(bean.getEquipProgram().get(k).getProgramList()==null||bean.getEquipProgram().get(k).getProgramList().size()==0){
					throw new ServiceException("第"+(k+1)+"行设备未选择项目");
				}
			}
			bean.setId(b.getId());
			equipCheckingService.addCheckingTask(bean);
		}else{
			bean.setId(b.getId());
			this.addCheckingTaskEquip(bean);
		}
	}

	@Override
	public void addCheckingTaskEquip(EquipCheckPlanBean bean) {
		//处理任务下的点检项目
		List<EquipCheckingTaskEquipBean> equipList = new ArrayList<>();
		if(bean.getEquipProgram()!=null&&bean.getEquipProgram().size()>0) {
			for (int j = 0; j < bean.getEquipProgram().size(); j++) {
				EquipCheckingTaskEquipBean p = new EquipCheckingTaskEquipBean();
				p.setProgramList(bean.getEquipProgram().get(j).getProgramList());
				p.setPlanId(bean.getId());
				p.setEquipId(bean.getEquipProgram().get(j).getEquipId());
				p.setClassId(bean.getEquipProgram().get(j).getClassId());
				p.setEquipCode(bean.getEquipProgram().get(j).getEquipCode());
				p.setEquipName(bean.getEquipProgram().get(j).getEquipName());
				p.setClassName(bean.getEquipProgram().get(j).getClassName());
				p.setEquipModel(bean.getEquipProgram().get(j).getEquipModel());
				p.setCreateTime(new Date());
				p.setLastUpdateTime(new Date());
				p.setCreateUser(SpringWebUtils.getRequestUser());
				p.setLastUpdateUser(SpringWebUtils.getRequestUser());
				p.setCompanyCode(SpringWebUtils.getRequestCompany());
				equipList.add(p);
			}
			//保存任务关联的设备
			if (equipList != null && equipList.size() > 0) {
				equipCheckingTaskDao.addEquipCheckingTaskEquip(equipList);
			}
		}
		//设备中选择的项目
		List<EquipCheckingTaskProgram> programList = new ArrayList<>();
		for (int j = 0; j < equipList.size(); j++) {
			if(equipList.get(j).getProgramList()!=null&&equipList.get(j).getProgramList().size()>0) {
				for (int k = 0; k < equipList.get(j).getProgramList().size(); k++) {
					EquipCheckingTaskProgram p = new EquipCheckingTaskProgram();
					p.setTaskId(equipList.get(j).getTaskId());
					p.setEquipId(equipList.get(j).getEquipId());
					p.setTaskEquipId(equipList.get(j).getId());
					p.setClassId(equipList.get(j).getClassId());
					p.setEquipCode(equipList.get(j).getEquipCode());
					p.setEquipName(equipList.get(j).getEquipName());
					p.setClassName(equipList.get(j).getClassName());
					p.setProgramId(equipList.get(j).getProgramList().get(k).getProgramId());
					p.setProgramName(equipList.get(j).getProgramList().get(k).getProgramName());
					p.setCheckPurpose(equipList.get(j).getProgramList().get(k).getCheckPurpose());
					p.setCreateTime(new Date());
					p.setLastUpdateTime(new Date());
					p.setCreateUser(SpringWebUtils.getRequestUser());
					p.setLastUpdateUser(SpringWebUtils.getRequestUser());
					p.setCompanyCode(SpringWebUtils.getRequestCompany());
					programList.add(p);
				}
			}
		}
		//保存点检项目
		if (programList != null && programList.size() > 0) {
			equipCheckingTaskDao.addEquipCheckingTaskProgram(programList);
		}
	}

	@Override
	public void deleteCheckingTaskEquip(EquipCheckPlanBean bean) {
		//查询出当前计划下的任务设备
		List<EquipCheckingTaskEquipBean> list=equipCheckingTaskDao.selectCheckTaskProgramByIds(bean.getId());

		//删除计划下的任务设备
		equipCheckingTaskDao.deleteEquipCheckingTaskEquip(bean.getId());

		//删除任务设备关联的项目
		if(list!=null&&list.size()>0) {
			equipCheckingTaskDao.deleteEquipCheckingTaskProgram(list);
		}

	}

	/**
	 *	查询设备点检任务项目通过设备点检计划id
	 * */
	@Override
	public List<EquipCheckingTaskEquipBean> selectCheckingTaskProgramById(Integer id) {
		return equipCheckingTaskDao.selectCheckTaskProgramByIds(id);
	}




}
